package com.wxt.ssm.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.wxt.ssm.bean.Employee;
import com.wxt.ssm.bean.Msg;
import com.wxt.ssm.service.EmployeeService;
import com.github.pagehelper.PageInfo;
@Controller
public class EmployeeController {
	
	@Autowired
	EmployeeService employeeService;
	
	@RequestMapping(value="del/{ids}",method=RequestMethod.DELETE)
	@ResponseBody
	
	public Msg delEmpbyid(@PathVariable(value="ids")String ids) {
		if(ids.contains("-")) {
			String [] str_ids=ids.split("-");
			List<Integer> list=new ArrayList<Integer>();
			for(String str:str_ids) {
				
				list.add(Integer.parseInt(str));
				
			}
			employeeService.delempbylist(list);
			return Msg.succes();
		}else {
			Integer id=Integer.parseInt(ids);
		employeeService.delempbyid(id);
		return Msg.succes();
		}
		
	}
	
	
	
	
	@RequestMapping(value="update/{empId}",method=RequestMethod.PUT)
	@ResponseBody
	
	public Msg updataEmp(Employee employee) {
		employeeService.updataEmp(employee);
		System.out.println(employee);
		
		return Msg.succes();
	}
	
	
	
	@RequestMapping(value="/emp/{Id}",method=RequestMethod.GET)
	@ResponseBody
	public Msg getEmp(@PathVariable("Id") Integer Id) {
		
		Employee emp=	employeeService.getEmp(Id);
		return Msg.succes().add("emp", emp);
		
	}
	
	@RequestMapping(value="/chenkUser",method=RequestMethod.POST)
	@ResponseBody
	
	public Msg chenkUser( @RequestParam(value="empNume")String empNume) {
		String regtx="^[a-zA-Z0-9_-]{6,16}$|(^[\\u2E80-\\u9FFF]{2,5}$)"; 
		if(!empNume.matches(regtx)) {
			return Msg.faill().add("vs_msg","不符合要求");
		}else {
		
		boolean b= employeeService.chenkuser(empNume);
		 if(b) {
			 return Msg.succes();
			 
		 }else {
			 return Msg.faill().add("vs_msg","用户不可用");
			 
		 }
		}
		
	}
	
	
	@RequestMapping(value="/emps",method=RequestMethod.POST)
	@ResponseBody
	public Msg saveEmps(Employee employee) {
		employeeService.saveEmp(employee);
		return Msg.succes();
		
	}
	
	/*获取全部人员列表*/
	
	@RequestMapping("/emps")
	@ResponseBody
	Msg getEmps(@RequestParam(value="pn",defaultValue="1")Integer pn){
		PageHelper.startPage(pn, 5);
		List<Employee> emps=employeeService.getAll();
		PageInfo <Employee> page=new PageInfo(emps,5);
		return Msg.succes().add("pageinfo",page);
		
	}
	
	/*@RequestMapping("/emps")
	public String getEmps(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model){
		
	PageHelper.startPage(pn, 5);
	List<Employee> emps=employeeService.getAll();
	PageInfo <Employee> page=new PageInfo<>(emps);
	model.addAttribute("pageinfo",page);
		return "list2";
		
	}*/
	

}
